package com.alibaba.citrus.cr.unified.inventory.repository.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


import com.alibaba.citrus.cr.unified.inventory.repository.InvoiceOrderReadRepository;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.common.util.JsonUtil;
import com.alibaba.cz.base.tool.feature.Features;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crunifiedinventory.domain.invoiceorder.dto.LoadRequest;
import com.epoch.app.crunifiedinventory.domain.invoiceorder.dto.SearchRequest;
import com.epoch.app.crunifiedinventory.domain.invoiceorder.model.InvoiceOrder;
import com.epoch.app.crunifiedinventory.domain.invoiceorder.service.InvoiceOrderService;

import com.epoch.app.crunifiedinventory.domain.invoiceorderdetail.model.InvoiceOrderDetail;
import com.epoch.app.crunifiedinventory.domain.invoiceorderdetail.service.InvoiceOrderDetailService;
import com.epoch.app.crunifiedinventory.dto.request.InvoiceOrderQueryRequest;
import com.epoch.app.crunifiedinventory.model.dto.InvoiceDetailRequest;
import com.epoch.app.crunifiedinventory.sdo.InvoiceOrderDetailSDO;
import com.epoch.app.crunifiedinventory.sdo.InvoiceOrderSDO;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class InvoiceOrderReadRepositoryImpl implements InvoiceOrderReadRepository {
    @Resource
    private InvoiceOrderService invoiceOrderService;

    @Resource
    private InvoiceOrderDetailService invoiceOrderdetailService;

    @Override
    public Result<List<InvoiceOrderSDO>> queryInvoiceOrderList(InvoiceOrderQueryRequest request) {
        SearchRequest searchRequest = new SearchRequest();
        List<InvoiceOrderSDO> invoiceOrderSDOList = Lists.newArrayList();
        if (StringUtils.isNotBlank(request.getInvoiceOrderId())){
            searchRequest.setInvoiceOrderId(request.getInvoiceOrderId());
            Result<List<InvoiceOrder>> search = invoiceOrderService.search(searchRequest);
            if (search != null && CollectionUtils.isNotEmpty(search.getResult())){
                invoiceOrderSDOList = JsonUtil.convertList(search.getResult(),InvoiceOrderSDO.class);
                return Result.success(invoiceOrderSDOList);
            }
        }else if(StringUtils.isNotBlank(request.getScItemId())){
//            InvoiceOrderDetailLoadListRequest invoiceOrderDetailLoadListRequest = new InvoiceOrderDetailLoadListRequest();
//            invoiceOrderDetailLoadListRequest.setScItemId(request.getScItemId());
            InvoiceOrderDetail invoiceOrderDetail = new InvoiceOrderDetail();
            invoiceOrderDetail.setScItemId(request.getScItemId());
            Result<List<InvoiceOrderDetail>> search = invoiceOrderdetailService.search(invoiceOrderDetail);
            if (search != null && CollectionUtils.isNotEmpty(search.getResult())){
                List<String> ids =
                search.getResult().stream().filter(Objects::nonNull)
                        .filter(x->StringUtils.isNotBlank(x.getInvoiceOrderId()))
                        .map(InvoiceOrderDetail::getInvoiceOrderId).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(ids)){
                    searchRequest.setIds(ids);
                    Result<List<InvoiceOrder>> listResult = invoiceOrderService.search(searchRequest);
                    if (listResult != null && CollectionUtils.isNotEmpty(listResult.getResult())){
                        List<InvoiceOrderSDO> invoiceOrderSDOS = JsonUtil.convertList(listResult.getResult(),
                                InvoiceOrderSDO.class);
                        return Result.success(invoiceOrderSDOS);
                    }
                }

            }
        }

        return Result.success(invoiceOrderSDOList);

//        return  invoiceOrderService.loadList(request);
    }

    @Override
    public InvoiceOrderSDO queryInvoiceOrderById(String invoiceId) {
        LoadRequest request = new LoadRequest();
        request.setInvoiceOrderId(invoiceId);
        InvoiceOrder invoiceOrder = invoiceOrderService.load(request);
        InvoiceOrderSDO  sdo= convertInvoiceOrder2InvoiceOrderSDO(invoiceOrder);
        InvoiceDetailRequest invoiceDetailRequest = new InvoiceDetailRequest();
        invoiceDetailRequest.setInvoiceOrderId(invoiceId);
        Result<List<InvoiceOrderDetail>> detailList = invoiceOrderdetailService.loadList(invoiceDetailRequest);
        List<InvoiceOrderDetailSDO> detailSDOList=new ArrayList<>();
        detailList.getResult().forEach(x->{
            InvoiceOrderDetailSDO tmp=convertInvoiceOrderDetail2InvoiceOrderDetailSDO(x);
            detailSDOList.add(tmp);
        });
        sdo.setDetailList(detailSDOList);
        return sdo;
    }

    private InvoiceOrderSDO convertInvoiceOrder2InvoiceOrderSDO(InvoiceOrder invoiceOrder) {
        InvoiceOrderSDO invoiceOrderSDO = new InvoiceOrderSDO();
        invoiceOrderSDO.setInvoiceOrderId(invoiceOrder.getInvoiceOrderId());
        invoiceOrderSDO.setReverseInvoiceId(invoiceOrder.getReverseInvoiceId());
        invoiceOrderSDO.setType(invoiceOrder.getType());
        invoiceOrderSDO.setSaleOrganizationCode(invoiceOrder.getSaleOrganizationCode());
        invoiceOrderSDO.setDeliveryOrderId(invoiceOrder.getDeliveryOrderId());
        invoiceOrderSDO.setSourceOrderId(invoiceOrder.getSourceOrderId());
        invoiceOrderSDO.setSapGenerateTime(invoiceOrder.getSapGenerateTime());
        invoiceOrderSDO.setCreateDate(invoiceOrder.getCreateDate());
        invoiceOrderSDO.setCompanyCode(invoiceOrderSDO.getCompanyCode());
        invoiceOrderSDO.setFeature(Features.newFeatures(invoiceOrder.getFeature()).getOriginMap());
        invoiceOrderSDO.setCreator(invoiceOrderSDO.getCreator());
        return invoiceOrderSDO;
    }
    private InvoiceOrderDetailSDO convertInvoiceOrderDetail2InvoiceOrderDetailSDO(
            InvoiceOrderDetail detail) {
        InvoiceOrderDetailSDO invoiceOrderDetail =new InvoiceOrderDetailSDO();
        invoiceOrderDetail.setInvoiceOrderId(detail.getInvoiceOrderId());
        invoiceOrderDetail.setOrderLineId(String.valueOf(detail.getOrderLineId()));
        invoiceOrderDetail.setScItemId(detail.getScItemId());
        invoiceOrderDetail.setId(detail.getId());
        invoiceOrderDetail.setTitle(detail.getTitle());
        invoiceOrderDetail.setPrice(detail.getPrice());
        invoiceOrderDetail.setQuantity(detail.getQuantity());
        invoiceOrderDetail.setSubTotalAmount(detail.getSubTotalAmount());
        invoiceOrderDetail.setReturnQuantity(detail.getReturnQuantity());
        invoiceOrderDetail.setQuantity(detail.getQuantity());
        invoiceOrderDetail.setDeliveryOrderId(detail.getDeliveryOrderId());
        invoiceOrderDetail.setSourceOrderId(detail.getSourceOrderId());
        invoiceOrderDetail.setFeature(Features.newFeatures(detail.getFeature()).getOriginMap());
        return invoiceOrderDetail;
    }

}
